package lobystate
{
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.core.FlexGlobals;
	
	import poker.LevelDefine;

	public class StateGetRoomPlayerlist extends NetRequestState
	{
		private static var instance:StateGetRoomPlayerlist = null;
		//
		[Bindable]
		public var roomlist:ArrayCollection = new ArrayCollection();

		public function StateGetRoomPlayerlist()
		{
			super();
		}

		public static function get Instance():StateGetRoomPlayerlist
		{
			if(instance == null)
				instance = new StateGetRoomPlayerlist();
			return instance;
		}

		override public function send(obj:StateManager):void
		{
			LobyNetManager.Instance.httpservice.url = LobyNetManager.URL_lobysonAddress;
			LobyNetManager.Instance.httpservice.request = {"PROCESS":LobyNetManager.URL_roomPlayerlist};
			LobyNetManager.Instance.httpservice.send();
		}
		override public function receive(obj:Object):Boolean
		{
			if(super.receive(obj))
			{
				// 获得得分序列中，本大厅游戏的正确位置
//				var gid:int = StateGetTableInfo.Instance.gameRoomGid;
			/*	for(var m:int=0;m<roomlist.length;m++)
				{
					var player:Object = roomlist[m];
					var flag:Boolean = false;
					for(var i:int=0;i<obj.players.length;i++){
						if(player.nickname == obj.players[i].nickname){
							flag = true;
							player.money = obj.players[i].money;
							player.score = obj.players[i].score;
							for(var j:int=0; j<obj.players[i].score.length; j++){
								if(obj.players[i].score[j].id == gid){
									player.score = obj.players[i].score[j].score;
									break;
								}
							}
							player.level = LevelDefine.getLevelName(player.score);
							// destory this player
//							obj.players[i].nickname = "";
							break;
						}
					}
					if(flag)
					{
						
					}else {
						roomlist.removeItemAt(m);
						m--;
					}
				}*/
				roomlist.removeAll();
				for each(var leftplayer:Object in obj.players)
				{
					if(leftplayer.nickname != "")
					{
						var playerdata:Object = new Object();
						playerdata.userId = leftplayer.userId;
						playerdata.nickname = leftplayer.nickname;
						playerdata.score=leftplayer.score;						
						playerdata.money = leftplayer.money;
						playerdata.level = LevelDefine.getLevelName(playerdata.score);
						roomlist.addItem(playerdata);
					}
				}
				// 更新大厅的玩家数据列表
				FlexGlobals.topLevelApplication.roomplayerlist.invalidateList();
				// 继续对游戏的恢复
				if(LobyManager.Instance.state == 4)
				{
					// 继续进入游戏的请求， 请求一次当时离开的房间的信息
					LobyNetManager.Instance.send(LobyNetManager.getTableSetting);
				}
					
				return true;
			}
			else{
				return false;
			}
		}
		override public function fault(event:Event):void
		{
			trace("room list http error");
		}
	}
}